package org.eyeTopLight.service.impl;

import org.eyeTopLight.dao.UserDao;
import org.eyeTopLight.po.userEntity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MyUserDetailsService implements UserDetailsService {
    public static final Logger logger = LoggerFactory.getLogger(MyUserDetailsService.class);
    @Resource
    private UserDao userDao;

    @Override
    public UserDetails loadUserByUsername(String user_phone) throws UsernameNotFoundException {
        User searchUser = new User();
        searchUser.setUser_phone(user_phone);
        List<User> userList = null;
        try{
            userList = userDao.query(searchUser);
        }catch (Exception e){
            logger.error("查询数据库User表操作失败",e);
            throw new RuntimeException("数据库操作失败");
        }
        if(userList == null || userList.size() == 0){
            throw new UsernameNotFoundException("用户不存在");
        }
        return userList.get(0);

    }
}
